package com.demo.util.tree.utils;


import com.demo.util.CollUtil;
import com.demo.util.tree.beans.TreeVO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TreeUtil {

    public static <T extends TreeVO<T>> List<List<T>> getTreePathsList(Collection<T> columns) {
        List<List<T>> results = new ArrayList<>();
        getTreePathsList(columns, new ArrayList<>(), results);
        return results;
    }

    private static <T extends TreeVO<T>> void getTreePathsList(
            Collection<T> columns,
            List<T> parents,
            List<List<T>> results
    ) {
        for (T column : columns) {
            List<T> paths = CollUtil.push(parents, column);
            List<T> children = column.getChildren();
            if (CollUtil.isEmpty(children)) {
                results.add(paths);
            }
            else {
                getTreePathsList(children, paths, results);
            }
        }
    }

}
